/**
 * 
 */
package ifeng;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import util.StrUtil;

import com.wixc.entity.NewBean;
import com.wixc.util.ConstantsUtil;
import com.wixc.util.JdbcUtils;
import com.wixc.util.LoginUtil;

/**
 * @author weichuanxi
 * @date 2016年4月19日
 */
public class IFeng_News {
static ArrayList<NewBean> list = new ArrayList<NewBean>();
	
	public static void main(String[] args) {
		IFeng_ChinaNews_Run();
		IFeng_ForeignNews_Run();
		IFeng_SociaNews_Run();
		try {
			JdbcUtils.saveNews(list, "insert into news (id, title, detail, source, imageUrl,typeid,insertTime) values (?,?, ?,?,?,?, ?)");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void IFeng_ChinaNews_Run(){
		LoginUtil util = new LoginUtil();
		String html = util.Get("http://news.ifeng.com/listpage/11528/"+today()+"/1/rtlist.shtml");
		processor(util, html, ConstantsUtil.chinaNews);
	}
	
	public static void IFeng_ForeignNews_Run(){
		LoginUtil util = new LoginUtil();
		String html = util.Get("http://news.ifeng.com/listpage/11574/"+today()+"/1/rtlist.shtml");
		processor(util, html, ConstantsUtil.foreignNews);
	}
	public static void IFeng_SociaNews_Run(){
		LoginUtil util = new LoginUtil();
		String html = util.Get("http://news.ifeng.com/listpage/7837/"+today()+"/1/rtlist.shtml");
		processor(util, html, ConstantsUtil.socialNews);
	}
	
	private static ArrayList<NewBean> processor(LoginUtil util, String html, int socialNews) {
		Document doc = Jsoup.parse(html);
		Elements News = doc.select("div.left > div > ul> li> a");
		return deal(util, News, socialNews);
	}

	private static ArrayList<NewBean> deal(LoginUtil util, Elements News, int socialNews) {
		for (Element element : News) {
			// url title
			String url = element.select("a").attr("href");
			String name = element.text();

			String html = util.Get(url);
			Document doc = Jsoup.parse(html);
			// detail
			String detail = doc.select("#artical_real").outerHtml();
			Elements imgurls=doc.select("#artical_real").select("img");
			String imgurl=StrUtil.imgUrls("", imgurls);
			// 没有分页
//			Elements ul = doc.select("#pages").select("a");
//			if (ul.size() > 0) {
//				ul.remove(ul.size() - 1);
//				ul.remove(0);
//				for (Element element2 : ul) {
//					String nexturl = element2.attr("href");
//					String nhtml = util.Get(nexturl);
//					Document ndoc = Jsoup.parse(nhtml);
//					String ndetail = ndoc.select("#text").outerHtml();
//					detail += "<hr/>" + ndetail;
//				}
//			}

			NewBean news = new NewBean(0, name, detail, "凤凰新闻", imgurl, socialNews, new Date(System.currentTimeMillis()));
			list.add(news);
		}
		return list;
	}
	
	public static String today(){
		Date d = new Date(System.currentTimeMillis());  
		return d.toString().replace("-", "");
	}
}
